﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Reflection;
using KangkangTodoList;

namespace KangkangTodoList
{
    public delegate void ExchangeTitleDelegate(TodoItem item1, TodoItem item2);

    public delegate void ExchangeStringDelegate(string str1, string str2);

    public class TodoList
    {
        /// <summary>
        /// TodoItem集合
        /// </summary>
        public List<TodoItem> m_TodoList;

        public TodoList()
        {
            m_TodoList = new List<TodoItem>()
            {
                new TodoItem(){Title="TodoItem1",IsCompletedProperty=true},
                new TodoItem(){Title="TodoItem2",IsCompletedProperty=false},
                new TodoItem(){Title="TodoItem3",IsCompletedProperty=false}
            };
        }

        /// <summary>
        /// 删除某个TodoItem
        /// </summary>
        /// <param name="item"></param>
        public void RemoveTodoItem(TodoItem item)
        {
            if (m_TodoList.Contains(item))
            {
                m_TodoList.Remove(item);
            }
        }

        /// <summary>
        /// 增加某个TodoItem
        /// </summary>
        /// <param name="item"></param>
        public void AddTodoItem(TodoItem item)
        {
            m_TodoList.Add(item);
        }

        /// <summary>
        /// 更新某个TodoItem
        /// </summary>
        /// <param name="item"></param>
        /// <param name="title"></param>
        /// <param name="isCompleted"></param>
        public void UpdateTodoItem(TodoItem item, string title, bool isCompleted = false)
        {
            item.UpdateSelf(title, isCompleted);
        }
    }

    public class TodoItem : MonoBehaviour
    {
        /// <summary>
        /// 标题
        /// </summary>
        public string Title;

        /// <summary>
        /// 私有属性
        /// </summary>
        [SerializeField]
        private bool IsCompleted;

        public TodoItem()
        {
            
        }
        public TodoItem(string title,bool isCompleted)
        {
            Title = title;
            IsCompleted = isCompleted;
        }

        /// <summary>
        /// 属性封装
        /// </summary>
        public bool IsCompletedProperty
        {
            get { return IsCompleted; }
            set
            {
                if (value != IsCompleted)
                {
                    IsCompleted = value;
                    //to do something...
                }
            }
        }

        public void Init()
        {

        }

        public void SetTitile(string value)
        {
            Title = value;
        }

        /// <summary>
        /// 更新自身
        /// </summary>
        /// <param name="title"></param>
        /// <param name="isCompleted"></param>
        public void UpdateSelf(string title, bool isCompleted)
        {
            this.Title = title;
            this.IsCompletedProperty = isCompleted;
        }

    }
}

public class TodoListController : MonoBehaviour {

    private TodoList mTodoList;

    [SerializeField]
    private Text TodoListItemText;

    [SerializeField]
    private Transform TodoListContent;

	// Use this for initialization
	void Start () {
        TodoList todoList = new TodoList();
        mTodoList = todoList;
        var btn = transform.Find("AddTodoListButton").GetComponent<Button>();
        if (btn!=null)
        {
            btn.onClick.AddListener(AddTodoListBtn_Func);
        }

        //v0.0.3新增
        TodoListItemText = transform.Find("TodoListItem").GetComponent<Text>();
        TodoListContent = transform.Find("TodoListContent");

    }
	
	// Update is called once per frame
	void Update () {
    //    protected Transform TodoListContent;
    //protected TodoList mTodoList;
    //public void Init(IUIData uiData = null)
    //{
    //    mUIData = uiData;
    //    InitUI(uiData);
    //    RegisterUIEvent();

    //    TodoListContent = transform.Find("TodoListConten");
    //    TodoList todoList = new TodoList();
    //    mTodoList = todoList;
    //}
    //            AddTodoListButton.onClick.AddListener(AddTodoListBtn_Func);
    //private void AddTodoListBtn_Func()
    //{
    //    GameObject go = new GameObject();
    //    var todoItem = go.AddComponent<TodoItem>();
    //    mTodoList.AddTodoItem(todoItem);
    //    go.transform.SetParent(TodoListContent);
    //    todoItem.Title = "我是第" + mTodoList.m_TodoList.Count + "个TodoItem的标题";
    //    todoItem.name = "TodoItem " + mTodoList.m_TodoList.Count;
    //    TodoListItem_Title.text = todoItem.Title;

    //    Log.I("mTodoList内有{0}个TodoItem", mTodoList.m_TodoList.Count);
    //}
}

    private void AddTodoListBtn_Func()
    {
        //var todoItem = new TodoItem();
        //mTodoList.AddTodoItem(todoItem);
        //Debug.LogFormat("mTodoList里面有{0}个TodoItem", mTodoList.m_TodoList.Count);

        //v0.0.3新增
        GameObject go = new GameObject();
        var todoItem = go.AddComponent<TodoItem>();
        mTodoList.AddTodoItem(todoItem);
        go.transform.SetParent(TodoListContent);
        todoItem.Title = "我是新建的第" + mTodoList.m_TodoList.Count + "个TodoItem的标题";
        todoItem.name = "TodoItem " + mTodoList.m_TodoList.Count;
        TodoListItemText.text = todoItem.Title;
    }
}
